package com.example.memorypoint.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.memorypoint.R;
import com.example.memorypoint.entity.Msg;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{

    private List<Msg> list;

    public MsgAdapter(List<Msg> list){
        this.list = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        TextView leftMsg;
        ImageView leftAvatar;

        LinearLayout rightLayout;
        TextView rightMsg;
        ImageView rightAvatar;

        public ViewHolder(View view){
            super(view);
            leftLayout = view.findViewById(R.id.left_layout);
            leftMsg = view.findViewById(R.id.left_msg);
            leftAvatar = view.findViewById(R.id.left_avatar);

            rightLayout = view.findViewById(R.id.right_layout);
            rightMsg = view.findViewById(R.id.right_msg);
//            rightAvatar = view.findViewById(R.id.right_avatar);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Msg msg = list.get(position);
        if (msg.getType() == Msg.TYPE_RECEIVED) {
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.leftMsg.setText(msg.getContent());
            holder.leftAvatar.setImageResource(R.drawable.left_avatar);

            holder.rightLayout.setVisibility(View.GONE);
        } else if (msg.getType() == Msg.TYPE_SEND) {
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.rightMsg.setText(msg.getContent());
//            holder.rightAvatar.setImageResource(R.drawable.right_avatar);

            holder.leftLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
